<?php
/**
 * Template Name: My Venue
 *
 */
//wp_cache_post_change( get_the_ID() );
get_header(); ?>

<div class="container">
    <h1><?php the_title() ?></h1>

    <div class="row-fluid">
        <div class="span4">
            <div class="card grey">
                <h2 class="title">Venue Details</h2>

                <form id="my-details" style="margin:0;">
                    <? $venue = get_field('venue', 'user_'.get_user_id())  ?>
                    <input type="hidden" name='user' value="<? echo get_user_id() ?>" />
                    <input type="hidden" name='venue' value="<? echo $venue ?>" />
                    <!--
                    <fieldset>
                        <label>Username / Email</label>
                        <? echo get_userdata(get_user_id())->user_login;  ?>
                        <span class="help-block"></span>
                    </fieldset>
                    -->
                    <fieldset>
                        <label>Email</label>
                        <input type="text" name='email' value="<? echo get_userdata(get_user_id())->user_email;  ?>" />
                    </fieldset>
                    <fieldset>
                        <label>Venue Name</label>
                        <input type="text" name='name' value="<? echo get_field('name', $venue);  ?>" />
                    </fieldset>
                    <fieldset>
                        <label>Address</label>
                        <textarea name='address'><? echo get_field('address', $venue);  ?></textarea>
                    </fieldset>
                    <fieldset>
                        <label>Website</label>
                        <input type="text" name='website' value="<? echo get_field('website', $venue);  ?>" />
                    </fieldset>
                    <fieldset>
                        <label>Opening Times</label>
                        <input type="text" name='opening' value="<? echo get_field('opening', $venue);  ?>" />
                    </fieldset>
                    <fieldset>
                        <label>Primary Contact Email</label>
                        <input type="text" name='primary_contact_email' value="<? echo get_field('primary_contact_email', $venue);  ?>" />
                    </fieldset>
                    <fieldset>
                        <label>Primary Contact Number</label>
                        <input type="text" name='primary_contact_number' value="<? echo get_field('primary_contact_number', $venue);  ?>" />
                    </fieldset>
                    <fieldset>
                        <label>Primary Contact Name</label>
                        <input type="text" name='primary_contact_name' value="<? echo get_field('primary_contact_name', $venue);  ?>" />
                    </fieldset>
                    <fieldset>
                        <label>Other Contact Details</label>
                        <input type="text" name='other_contact_details' value="<? echo get_field('other_contact_details', $venue);  ?>" />
                    </fieldset>
                    <hr>

                    <fieldset>
                        <label>Nearest Station(s)</label>
                        <input type="text" name='nearest_station' value="<? echo get_field('nearest_station', $venue);  ?>" />
                    </fieldset>
                    <fieldset>
                        <label>Changing Rooms</label>
                        <select name='changing_rooms' value="<? echo get_field('changing_rooms', $venue);  ?>">
                            <option>Yes</option>
                            <option>No</option>
                        </select>
                    </fieldset>
                    <fieldset>
                        <label>Showers</label>
                        <select name='showers' value="<? echo get_field('showers', $venue);  ?>">
                            <option>Yes</option>
                            <option>No</option>
                        </select>
                    </fieldset>
                    <fieldset>
                        <label>Bar</label>
                        <select name='bar' value="<? echo get_field('bar', $venue);  ?>">
                            <option>Yes</option>
                            <option>No</option>
                        </select>
                    </fieldset>
                    <fieldset>
                        <label>Parking</label>
                        <select name='parking' value="<? echo get_field('parking', $venue);  ?>">
                            <option>Yes</option>
                            <option>No</option>
                        </select>
                    </fieldset>
                </form>

            </div>
        </div>
        <div class="span8">
            <div class="card grey">
                <a href="/my-calendar" class="btn btn-primary pull-right">Full Calendar &raquo;</a>
                <h2 class="title">Today's Bookings</h2>

                <table class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <th>Venue</th>
                        <th>Pitch</th>
                        <th>Start</th>
                        <th>End</th>
                        <th>Customer</th>
                        <th style="width: 69px;"></th>
                    </tr>
                    </thead>
                    <tbody>

                    <?
                    $count = 0;

                    $booking_query = new WP_Query(array('post_type' => 'booking', 'showposts' => 10000));
                    while ($booking_query->have_posts()) : $booking_query->the_post();
                        //$temp = get_post_meta($booking_query->post->ID, 'slot', true);
                        $slotid = get_field('slot', $booking_query->post->ID);
                        $pitchid = getPitchID($slotid);
                        $venueid = getVenueID($slotid);
                        $customerid = get_field('customer_id', $booking_query->post->ID);
                        if ($venueid == get_field('venue','user_'.get_user_id())) {
                            if (date('dmY',strtotime('today')) == date('dmY',strtotime(get_field('date', $booking_query->post->ID)))) {
                                $count++;
                                ?>

                            <tr>
                                <td><a href="<? echo get_permalink($venueid) ?>"><? the_field('name', $venueid); ?></a>
                                </td>
                                <td><a href="<? echo get_permalink($pitchid) ?>"><? the_field('pid', $pitchid); ?></a>
                                </td>
                                <td><? the_field('start_time', $slotid); ?></td>
                                <td><? the_field('end_time', $slotid); ?></td>
                                <td><? echo get_field('first_name', 'user_'.$customerid).' '.get_field('last_name', 'user_'.$customerid); ?></td>
                                <td><a href="<? echo get_permalink($booking_query->post->ID) ?>" class="btn btn-primary btn-small" style="font-weight: 800;margin-bottom: 0">Detail &raquo;</a></td>

                            </tr>
                                <?
                            }
                        }
                    endwhile;
                    if ($count == 0 ) {
                        ?>
                    <tr>
                        <td colspan="6" style="text-align: center">No Upcoming Bookings</td>
                    </tr>
                        <?
                    }
                    ?>
                    </tbody>
                </table>
            </div>
            <div class="card grey">
                <h2 class="title">Booking History</h2>

                <table class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <th>Venue</th>
                        <th>Pitch</th>
                        <th>Start</th>
                        <th>End</th>
                        <th>Date</th>
                        <th style="width: 69px;"></th>
                    </tr>
                    </thead>
                    <tbody>

                    <?
                    $count = 0;
                    $booking_query = new WP_Query(array('post_type' => 'booking', 'showposts' => 100));
                    while ($booking_query->have_posts()) : $booking_query->the_post();
                        //$temp = get_post_meta($booking_query->post->ID, 'slot', true);

                        $slotid = get_field('slot', $booking_query->post->ID);
                        $pitchid = getPitchID($slotid);
                        $venueid = getVenueID($slotid);
                        if (get_field('customer_id', $booking_query->post->ID) == get_user_id()) {
                            if (time() > strtotime(get_field('date', $booking_query->post->ID).'+ 1 day')) {
                                $count++;
                                ?>

                            <tr>
                                <td><a href="<? echo get_permalink($venueid) ?>"><? the_field('name', $venueid); ?></a>
                                </td>
                                <td><a href="<? echo get_permalink($pitchid) ?>"><? the_field('pid', $pitchid); ?></a>
                                </td>
                                <td><? the_field('start_time', $slotid); ?></td>
                                <td><? the_field('end_time', $slotid); ?></td>
                                <td><?

                                    $date = DateTime::createFromFormat('Ymd', get_field('date', $booking_query->post->ID));
                                    echo $date->format('l, d M Y');
                                    ?></td>
                                <td><a href="<? echo get_permalink($booking_query->post->ID) ?>" class="btn btn-primary btn-small" style="font-weight: 800;margin-bottom: 0">Detail &raquo;</a></td>

                            </tr>
                                <?
                            }
                        }
                    endwhile;
                    if ($count == 0 ) {
                        ?>
                    <tr>
                        <td colspan="6" style="text-align: center">No Previous Bookings</td>
                    </tr>
                        <?
                    }
                    ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>


<?php get_footer(); ?>

<script src="<?php echo get_template_directory_uri(); ?>/js/PW.myVenue.js"></script>
<script>
    $(function () {
        if (<?php
        if (is_user_logged_in()) {
            echo 'false';
        } else {
            echo 'true';
        }
        ?>) {
            $('#loginModal').modal({
                backdrop:'static '
            });
            $('#loginModal').on('hide', function () {
                return false;
            });
        }

        var options = {};
        PW.myVenue.init(options);
    });
</script>